package com.example.chatgptapplication.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.chatgptapplication.annotation.Log;
import com.example.chatgptapplication.entity.Admin;
import com.example.chatgptapplication.enums.BusinessType;
import com.example.chatgptapplication.enums.ResponseStatus;
import com.example.chatgptapplication.service.AdminService;
import com.example.chatgptapplication.utils.ImageUtil;
import com.example.chatgptapplication.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/admin")
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;


    @Autowired
    private ImageUtil imageUtil;


    @Log(title = "登录",businessType = BusinessType.OTHER)
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResponseData login(@RequestBody Admin admin){
        return adminService.adminLogin(admin.getUsername(),admin.getPassword()) == null ?
                new ResponseData(ResponseStatus.BAD_REQUEST.getCode(), ResponseStatus.BAD_REQUEST.getMessage(), null) :
                new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(),adminService.adminLogin(admin.getUsername(),admin.getPassword()));
    }

    @Log(title = "创建管理员",businessType = BusinessType.INSERT)
    @RequestMapping("/createAdmin")
    public ResponseData createAdmin(@RequestBody Admin admin){
        Admin user = adminService.createAdmin(admin);
        if(user == null){
            return new ResponseData(ResponseStatus.INTERNAL_SERVER_ERROR.getCode(), "用户已存在！", null);
        }
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), user);
    }

    @RequestMapping("/listAdmin")
    public ResponseData listAdmin(@RequestBody Admin admin){
        Page<Admin> adminPage = adminService.getAdminPageData(admin);
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), adminPage);
    }

    @Log(title = "修改管理员",businessType = BusinessType.UPDATE)
    @RequestMapping("/updateAdmin")
    public ResponseData updateAdmin(@RequestBody Admin admin){
        adminService.updateAdmin(admin);
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), null);
    }


    @Log(title = "删除管理员",businessType = BusinessType.DELETE)
    @RequestMapping("/deleteAdmin")
    public ResponseData deleteAdmin(@RequestBody Admin admin){
        adminService.deleteAdmin(admin);
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), null);
    }

    @RequestMapping("/addAvatar")
    public ResponseData fileUpload(@RequestBody MultipartFile file){
        String path = "E:\\project\\chatGptApplication\\src\\main\\resources\\static\\img\\";
        return new ResponseData(ResponseStatus.SUCCESS.getCode(), ResponseStatus.SUCCESS.getMessage(), imageUtil.uploadFile(file,path,"img"));
    }



}
